import factorys.LoggerFactory;
import strategys.LogStrategy;

import java.util.Scanner;

public class Calculator {
    private LogStrategy logger;

    public Calculator() {
        LoggerFactory loggerFactory = new LoggerFactory();
        logger = loggerFactory.getLogger();
    }

    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入第一个数字：");
            double num1 = scanner.nextDouble();
            System.out.println("请输入运算符（+ - * /）：");
            String operator = scanner.next();
            System.out.println("请输入第二个数字：");
            double num2 = scanner.nextDouble();

            try {
                double result = calculate(num1, operator, num2);
                System.out.println("结果：" + result);
                logger.log("INFO", num1 + " " + operator + " " + num2 + " = " + result, Thread.currentThread().getStackTrace());
            } catch (Exception e) {
                System.out.println("发生异常：" + e.getMessage());
                logger.log("ERROR", "异常：" + e.getMessage(), Thread.currentThread().getStackTrace());
            }
        }
    }

    private double calculate(double num1, String operator, double num2) {
        switch (operator) {
            case "+":
                return num1 + num2;
            case "-":
                return num1 - num2;
            case "*":
                return num1 * num2;
            case "/":
                if (num2 == 0) {
                    throw new ArithmeticException("除数不能为零");
                }
                return num1 / num2;
            default:
                throw new IllegalArgumentException("无效的运算符");
        }
    }

    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.run();
    }
}
